package vn.bk.hotel.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import vn.bk.hotel.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Other {

	private Context mContext;

	public Other(Activity myActivity) {
		mContext = myActivity;
	}
	
	 public static void CopyStream(InputStream is, OutputStream os)
	    {
	        final int buffer_size=1024;
	        try
	        {
	            byte[] bytes=new byte[buffer_size];
	            for(;;)
	            {
	              int count=is.read(bytes, 0, buffer_size);
	              if(count==-1)
	                  break;
	              os.write(bytes, 0, count);
	            }
	        }
	        catch(Exception ex){}
	    }

	public static Bitmap img(String url) {
		url = string2url(url);
		Bitmap bmp = null;

		try {
			bmp = BitmapFactory.decodeStream((InputStream) new URL(url)
					.getContent());

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}

		return bmp;

	}

	public Bitmap img(String url, Boolean isClickViewDetail) {
		url = string2url(url);

		Bitmap bmp = null;
		try {
			bmp = BitmapFactory.decodeStream((InputStream) new URL(url)
					.getContent());

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
		} catch (IOException e) {
			// TODO Auto-generated catch block
		}

		return bmp;
	}

	public void alert(String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
		builder.setTitle(mContext.getString(R.string.announce));
		builder.setMessage(message);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface arg0, int arg1) {
				// TODO Auto-generated method stub

			}
		});
		builder.show();
	}

	public static String string2url(String s) {
		s = s.replace(" ", "%20");
		s = s.replace("  ", "%20");
		return s;
	}

}
